package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.Issue;
import com.cskaoyan.bean.mallbean.IssueExample;
import com.cskaoyan.mapper.mallmapper.IssueMapper;
import com.cskaoyan.vo.mallvo.IssueListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/10 20:42
 */
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueListVo listIssue(Integer page, Integer limit, String question, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //查询
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(0);
        if (question!=null)
            criteria.andQuestionLike("%"+question+"%");
        issueExample.setOrderByClause(sort + " "+order); // order by add_time desc
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        //total
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        int size = issuePageInfo.getSize();

        IssueListVo issueListVo = new IssueListVo();
        issueListVo.setItems(issues);
        issueListVo.setTotal(size);
        return issueListVo;
    }

    @Override
    public Issue createIssue(String question, String answer) {

        Issue issue = new Issue(
          null,
                question,
                answer,
                new Date(),
                new Date(),
                false
        );
        issueMapper.insert(issue);

        return issue;
    }

    @Override
    public Issue updateIssue(Issue issue) {


        issueMapper.updateByPrimaryKeySelective(issue);
        Issue issueVo = issueMapper.selectByPrimaryKey(issue.getId());
        return issueVo;
    }

    @Override
    public void logicDeleteIssue(Integer id) {

        issueMapper.logicDeleteIssueById(id);

    }
}
